package demo.hdz.mediacodecdecode.customview;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;

import demo.hdz.mediacodecdecode.Logger;
import demo.hdz.mediacodecdecode.Util;
import demo.hdz.mediacodecdecode.interfaces.IDecodeManager;
import demo.hdz.mediacodecdecode.interfaces.IDecodeOperate;

/**
 * @author hdz
 * @date 2022/05/27 19:24
 */
public class VideoGLTextureView extends TextureView implements IVideoView {
    private static final String TAG = "VideoGLTextureView";
    private static int mScreenWidth, mScreenHeight, mVideoWidth, mVideoHeight;
    private SurfaceTexture mSurfaceTexture = null;
    private IDecodeOperate mDecodeOperate = null;

    private TextureViewRenderer mRenderer;

    private VideoViewUtil mVideoViewUtil;

    public VideoGLTextureView(Context context) {
        super(context);
        init();
    }

    public VideoGLTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public static void setSize(int screenWidth, int screenHeight, int videoWidth, int videoHeight) {
        Logger.d(TAG + ",setSize: " + screenWidth + "x" + screenHeight + " " + videoWidth + "x" + videoHeight);
        mScreenWidth = screenWidth;
        mScreenHeight = screenHeight;
        mVideoWidth = videoWidth;
        mVideoHeight = videoHeight;
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            if (mScreenWidth < mScreenHeight) {
                int tmp = mScreenWidth;
                mScreenWidth = mScreenHeight;
                mScreenHeight = tmp;
            }
        } else {
            if (mScreenWidth > mScreenHeight) {
                int tmp = mScreenWidth;
                mScreenWidth = mScreenHeight;
                mScreenHeight = tmp;
            }
        }
        mVideoViewUtil.adjustSize(this, mScreenWidth, mScreenHeight, mVideoWidth, mVideoHeight);
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        Logger.d(TAG + ",onWindowVisibilityChanged: " + Util.visibility2String(visibility));
    }

    private void init() {
        mVideoViewUtil = new VideoViewUtil();
        mRenderer = new TextureViewRenderer();
        setSurfaceTextureListener(new SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                Logger.e(TAG, "onSurfaceTextureAvailable: " + width + "x" + height);
                mScreenWidth = width;
                mScreenHeight = height;
                mVideoViewUtil.adjustSize(VideoGLTextureView.this, mScreenWidth, mScreenHeight, mVideoWidth, mVideoHeight);
                mRenderer.init(VideoGLTextureView.this, getContext());
                mSurfaceTexture = mRenderer.initOESTexture();
                Logger.e(TAG + ",init->onSurfaceTextureAvailable: " + mSurfaceTexture);
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                Logger.d(TAG, "onSurfaceTextureSizeChanged: " + width + "," + height);
                mScreenWidth = width;
                mScreenHeight = height;
                mVideoViewUtil.adjustSize(VideoGLTextureView.this, mScreenWidth, mScreenHeight, mVideoWidth, mVideoHeight);
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {

                stop();

                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
    }

    @Override
    public boolean isAvailable() {

        Logger.d(TAG, "isAvailable: " + super.isAvailable() + "," + mSurfaceTexture);

        return super.isAvailable() && (mSurfaceTexture != null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Logger.d(TAG + ",onTouchEvent: " + event.toString());
        float x = event.getRawX();
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (x > mScreenWidth / 2) {
                Logger.d(TAG + ",onTouchEvent: 暂停");
                startOrPause(true);
            } else {
                Logger.d(TAG + ",onTouchEvent: 开始");
                startOrPause(false);
            }
        }
        return true;
    }

    @Override
    public Surface getSurface() {
        if (mSurfaceTexture != null) {
            return new Surface(mSurfaceTexture);
        }
        return null;
    }


    @Override
    public void setListener(IDecodeOperate o) {
        mDecodeOperate = o;
    }

    @Override
    public void startOrPause(boolean pause) {
        if (mDecodeOperate != null) {
            mDecodeOperate.startOrPause(pause);
        }
    }

    @Override
    public void stop() {
        if (mDecodeOperate != null) {
            mDecodeOperate.stop();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mRenderer != null) {
            mRenderer.release();
        }
    }
}
